package com.vacation.vacationdemo.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.vacation.vacationdemo.realm.VacationRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 杨光
 * @date 2022年03月12日 15:53
 */
@Configuration
public class ShiroConfig {
    /**
     * 配置ShiroDialect，用于Shiro和thymeleaf标签配合使用
     * @return
     */
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }


    //将自己的验证方式加入容器
    @Bean
    public VacationRealm myShiroRealm() {
        VacationRealm vacationRealm = new VacationRealm();
        return vacationRealm;
    }

    //权限管理，配置主要是Realm的管理认证
    //注意：这里的SecurityManager需要的是shiro提供的，不是jdk自带的
    //import org.apache.shiro.mgt.SecurityManager;
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        return securityManager;
    }
    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> map = new LinkedHashMap<>();

        Map<String, Filter> filtersMap  = new LinkedHashMap<>();
        //过滤器种类
//        1.anon 无需认证
//        2.authc 必须认证
//        3.user 记住我可以访问
//        4.perms 授予权限才可以访问
//        5.role 授予角色才可以访问

        //登录
        shiroFilterFactoryBean.setLoginUrl("/vacation/tologin");
        //登录成功后的跳转页面
        shiroFilterFactoryBean.setSuccessUrl("/vacation/toindex");
        //错误页面，没有权限调用某个url
        shiroFilterFactoryBean.setUnauthorizedUrl("/vacation/unauth");

        //自定义登出后跳转的url
        LogoutFilter logoutFilter = new LogoutFilter();
        logoutFilter.setRedirectUrl("http://www.baidu.com");
        filtersMap.put("logoutFilter", logoutFilter);
        shiroFilterFactoryBean.setFilters(filtersMap);

        //配置哪些资源路径需要拦截，哪些不需要
        //登出
        map.put("/vacation/logout", "logoutFilter");
        map.put("/vacation/toindex","anon");
        map.put("/vacation/tologin","anon");
        map.put("/vacation/dologin","anon");

        map.put("/js/**","anon");
        map.put("/css/**","anon");

        //授予权限
        map.put("/vacation/toadd","perms[vacation:toadd]");
        map.put("/vacation/add","perms[vacation:add]");
        map.put("/vacation/employee","perms[vacation:employee]");
        map.put("/vacation/cancel/apply","perms[vacation:cancel:apply]");
        map.put("/vacation/search","perms[vacation:search]");
        map.put("/vacation/export","perms[vacation:export]");

        //注意：这个是验证所有请求都需要登录的，把他放在最后
        //对所有用户认证
        map.put("/**", "authc");



        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);


        return shiroFilterFactoryBean;
    }

}
